package connect4client.commands;

import game.Player;
import messages.InvitationMessage;
import connect4client.core.*;

/**
 * The Invite command.
 * 
 * This command will handle Invite messages.
 */
public class InvitationCommand extends Command {

	public InvitationCommand(InvitationMessage inviteMessage) {
		super(inviteMessage);
	}


	@Override
	public void execute() {
		InvitationMessage message = (InvitationMessage) this.message;

		GameManager gameManager = controller.getGameManager();
		PlayerManager playerManager = controller.getPlayerManager();

		if (controller.getUser().getLogin().equals(message.getOriginLogin())) {
			Player player = playerManager.getPlayerByLogin(message.getDestinyLogin());
			Invitation invitation = new Invitation(controller.getUser().getPlayer(), player, message.getGameName());
			gameManager.addInvitation(message.getReplyId(), invitation);
		} else {
			Player player = playerManager.getPlayerByLogin(message.getOriginLogin());
			Invitation invitation = new Invitation(player, controller.getUser().getPlayer(), message.getGameName());
			gameManager.addInvitation(message.getReplyId(), invitation);

			String opponentName = message.getOriginLogin();
			String gameName = message.getGameName();
			int replyId = message.getReplyId();

			guiMediator.invitationReceived(opponentName, gameName, replyId);
		}
	}

}
